/********************************************
 * 功能说明: Mybatis实体生成类
 * 模块名称: 生成模块
 * 系统名称: java敏捷开发通用平台
 * 软件版权:
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2017年7月14日 下午1:48:50
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.database.mybatis.generator;

import com.hyacinth.database.mybatis.generator.template.Template;
import com.hyacinth.utils.JDBCUtils;
import com.hyacinth.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Mybatis实体生成类
 * @author zhangfb
 * @version 1.0.0.1
 */
public class EntityGenerator extends BaseGenerator {
	
	private String tableName;
	private String[] ignoreArray = {"id", "code", "name", "status"};
	private List<ColumnField> columnList = new ArrayList<ColumnField>();
	
	public static void main(String[] args) {
		EntityGenerator generator = new EntityGenerator();
		generator.setTableName("hoyi_subsidy_rule");
		generator.setModuleName("银行模块");
		generator.setPackageName("com.aghoo.ifpbiz.modules.bank.entity");
		generator.execute();
	}
	
	// 初始化
	public void init() {
		// 初始化字段列表
		initColumnFieldList();
		setTempType("mybatis.entity");
	}
	
	/**
	 * 增加替换参数
	 * @return
	 */
	public void addTemplateParams(Template template) {
		template.put("${tableName}", tableName);
	}
	
	/**
	 * 生成字段内容
	 * @return
	 */
	public String getFieldContent() {
		StringBuilder sb = new StringBuilder();
		for (ColumnField column : columnList) {
			/** 字典条目列表 */
			sb.append(String.format("@FieldMeta(name = \"%s\")\r\n\t", column.getColumnComment()));
			if ("Date".equals(column.getFieldType())) {
				sb.append(String.format("@JsonFormat(pattern=\"yyyy-MM-dd HH:mm:ss\")\r\n\t", column.getColumnComment()));
			}
			sb.append(String.format("private %s %s;\r\n\t", column.getFieldType(), column.getFieldName()));
		}
		return sb.toString();
	}
	
	/**
	 * 生成方法内容
	 * @return
	 */
	public String getMethodContent() {
		StringBuilder sb = new StringBuilder();
		for (ColumnField column : columnList) {
			/** 字典条目列表 */
			sb.append(String.format("public %s %s {\r\n\t\treturn %s;\r\n\t}\r\n\t", column.getFieldType(), column.getGetMethod(), column.getFieldName()));
			sb.append(String.format("public void %s {\r\n\t\tthis.%s = %s;\r\n\t}\r\n\t", column.getSetMethod(), column.getFieldName(), column.getFieldName()));
		}
		return sb.toString();
	} 
	
	/**
	 * 获取字段列表
	 * @return
	 */
	public void initColumnFieldList() {
		if (columnList.size() > 0) {
			return;
		}
		ResultSet rs = JDBCUtils.query("SHOW FULL COLUMNS FROM " + tableName);
		try {
			while (rs.next()) {
				ColumnField column = new ColumnField();
				column.setColumnName(rs.getString("FIELD"));
				if (ArrayUtils.contains(ignoreArray, column.getColumnName())) {
					continue;
				}
				column.setColumnType(rs.getString("TYPE"));
				column.setFieldName(StringUtils.toCamelCase(column.getColumnName()));
				column.setColumnComment(rs.getString("COMMENT"));
				if ("PRI".equals(rs.getString("KEY"))) {
					column.setPrikey(true);
				}
				if (column.getColumnType().startsWith("varchar")) {
					column.setFieldType("String");
				} else if (column.getColumnType().startsWith("int")) {
					column.setFieldType("Integer");
				} else if (column.getColumnType().startsWith("bigint")) {
					column.setFieldType("Long");
				} else if (column.getColumnType().startsWith("datetime")) {
					column.setFieldType("Date");
					if (!importList.contains("import java.util.Date;")) {
						importList.add("import java.util.Date;");
						importList.add("import com.fasterxml.jackson.annotation.JsonFormat;");
					}
				} else if (column.getColumnType().startsWith("decimal")) {
					column.setFieldType("BigDecimal");
					if (!importList.contains("import java.math.BigDecimal;")) {
						importList.add("import java.math.BigDecimal;");
					}
				} else if (column.getColumnType().equals("tinyint(1)")) {
					column.setFieldType("Boolean");
				}
				String name = column.getFieldName().substring(0, 1).toUpperCase() + column.getFieldName().substring(1);
				column.setGetMethod("get" + name + "()");
				column.setSetMethod("set" + name + "("+column.getFieldType()+" " + column.getFieldName() + ")");
				columnList.add(column);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(rs);
		}
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
		this.javaName = getJavaName(tableName);
		this.fileName = this.javaName + ".java";
		this.className = getClassName(tableName);
	}
	
	public String getJavaName(String tableName) {
		String className = tableName;
		if (tableName.startsWith("hoyi_")) {
			className = tableName.substring(5);
		}
		className = StringUtils.toCamelCase(className);
		return className.substring(0, 1).toUpperCase() + className.substring(1);
	}
	
	/**
	 * 获取表注释
	 * @param tableName
	 * @return
	 */
	public String getClassName(String tableName) {
		String tableComment = null;
		ResultSet rs = JDBCUtils.query("SHOW TABLE STATUS LIKE ? ", tableName);
		try {
			if (rs.next()) {
				tableComment = rs.getString("COMMENT");
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return tableComment;
	}

}
